package com.webinf.controller;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.service.UserModelService;
import com.webinf.entity.WebInfo;
import com.webinf.service.WebInfoDao;

@Controller
@RequestMapping("/webinfo")
public class WebInfoController {
	
	@Autowired
	private WebInfoDao ckpd;
	
	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(WebInfo webInf){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			WebInfo inf =ckpd.save(webInf);
			map.put("data", inf);
			map.put("message", "保存成功！");
			map.put("status",0);
			//发送websocket 信息
	/*		Map<String, List<WebSocketSession>> mapSesson = WebRTCWebSocketHandler.allConnections;
			List<WebSocketSession> list = mapSesson.get("N005");
			if (list!=null) {
				for (WebSocketSession session : list) {
					//JSON event时间
					JSONObject json = new JSONObject();
					json.element("type", "get_doctor_result ");
					json.put("data", inf);
					System.out.println("发送诊断信息=="+json.toString());
					WebSocketMessage<?> message = new TextMessage(json.toString().getBytes());
					session.sendMessage(message);
				}
			}*/
			
			//第二种发方式发送websocket
			JSONObject json = new JSONObject();
			json.element("type", "get_doctor_result");
			json.put("data", inf);
			System.out.println("发送诊断信息=="+json.toString());
			UserModelService.sendMessageToAllUser(json.toString());
			
			
	
		
			
		} catch (Exception e) {
			e.printStackTrace();
			map.put("data", null);
			map.put("message", "保存失败！");
			map.put("status",1);
		}
		return map;
	}
	
	
	@RequestMapping("/findList")
	@ResponseBody
	public Map<String,Object> findList(String userId){
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> map1 = new HashMap<String, Object>();
		try {
			
			  Page<WebInfo> page =  ckpd.findAll(userId,new PageRequest(0, 5));
			  map1.put("list", page.getContent());
			  map.put("data", map1);
			map.put("message", "查询成功！");
			map.put("status",0);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("data", null);
			map.put("message", "查询失败！");
			map.put("status",1);
		}
		return map;
	}
}
